package com.yunxi.controller.finance;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.finance.pojo.DownPaymentAmountComputeReqVO;
import com.yunxi.finance.pojo.FinancialComputeColumnsEditVO;
import com.yunxi.finance.pojo.FinancialComputeColumnsReqVO;
import com.yunxi.finance.pojo.FinancingPrincipalComputeReqVO;
import com.yunxi.finance.pojo.LeaseContractAmountComputeReqVO;
import com.yunxi.finance.pojo.LeaveEnoughAmountComputeReqVO;
import com.yunxi.finance.pojo.MarginAmountComputeReqVO;
import com.yunxi.finance.pojo.MonthlyRentComputeReqVO;
import com.yunxi.finance.service.FinanceComputeDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/finance/compute")
@Api(tags = "finance:financeCompute", description = "金融计算器")
public class FinanceComputeController {

    @Autowired
    FinanceComputeDubboService financeComputeDubboService;

    /**
     * 金融计算器项目分页查询
     *
     * @param queryVO 查询条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ApiResult page(FinancialComputeColumnsReqVO queryVO, Integer pageNum, Integer pageSize) {
        if (StringUtils.isEmpty(queryVO.getType())) {
            return ResponseUtil.fail(-1, "类型不能为空");
        }
        queryVO.setIsSpecial(0);
        return financeComputeDubboService.page(queryVO, pageNum, pageSize);
    }

    /**
     * 金融计算器项目不分页查询
     *
     * @param queryVO 查询条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation(value = "不分页查询")
    public ApiResult list(FinancialComputeColumnsReqVO queryVO) {
        if (StringUtils.isEmpty(queryVO.getType())) {
            return ResponseUtil.fail(-1, "类型不能为空");
        }
        return financeComputeDubboService.list(queryVO);
    }

    /**
     * 根据ID查询
     *
     * @param id id
     * @return 查询结果
     */
    @GetMapping("/load")
    @ApiOperation(value = "单条查询")
    public ApiResult load(String id) {
        return financeComputeDubboService.load(id);
    }

    /**
     * 金融计算器项目不分页查询
     *
     * @param editVO edit VO
     * @return 查询结果
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public ApiResult save(@RequestBody FinancialComputeColumnsEditVO editVO) {
        return financeComputeDubboService.save(editVO);
    }


    /**
     * 计算融资租赁合同金额
     *
     * @param vo 成交价格 + 附件包价格 + GPS价格
     * @return 融资租赁合同金额
     */
    @PostMapping("/computeLeaseContractAmount")
    @ApiOperation(value = "计算融资租赁合同金额")
    public ApiResult computeLeaseContractAmount(@RequestBody LeaseContractAmountComputeReqVO vo) {
        return ResponseUtil.ok(financeComputeDubboService.computeLeaseContractAmount(vo));
    }

    /**
     * 计算首付款金额
     *
     * @param vo 融资租赁合同金额 * 首付款比例
     * @return 首付款金额
     */
    @PostMapping("/computeDownPaymentAmount")
    @ApiOperation(value = "计算首付款金额")
    public ApiResult computeDownPaymentAmount(@RequestBody DownPaymentAmountComputeReqVO vo) {
        return ResponseUtil.ok(financeComputeDubboService.computeDownPaymentAmount(vo));
    }

    /**
     * 计算保证金金额
     *
     * @param vo 融资租赁合同金额 * 保证金比例
     * @return 保证金金额
     */
    @PostMapping("/computeMarginAmount")
    @ApiOperation(value = "计算保证金金额")
    public ApiResult computeMarginAmount(@RequestBody MarginAmountComputeReqVO vo) {
        return ResponseUtil.ok(financeComputeDubboService.computeMarginAmount(vo));
    }

    /**
     * 计算留购价金额(尾款)
     */
    @PostMapping("/computeLeaveEnoughAmount")
    @ApiOperation(value = "计算留购价金额(尾款)")
    public ApiResult computeLeaveEnoughAmount(@RequestBody LeaveEnoughAmountComputeReqVO vo) {
        return ResponseUtil.ok(financeComputeDubboService.computeLeaveEnoughAmount(vo));
    }

    /**
     * 计算月租金（含税） 月租金 = 本金*月利率*(1+月利率)^n/[(1+月利率)^n-1]
     */
    @PostMapping("/computeMonthlyRent")
    @ApiOperation(value = "计算月租金（含税）")
    public ApiResult computeMonthlyRent(@RequestBody MonthlyRentComputeReqVO vo) {
        return ResponseUtil.ok(financeComputeDubboService.computeMonthlyRent(vo));
    }

    /**
     * 融资本金 = 融资租赁合同金额- 首付金金额
     */
    @PostMapping("/computeFinancingPrincipal")
    @ApiOperation(value = "计算融资本金")
    public ApiResult computeFinancingPrincipal(@RequestBody FinancingPrincipalComputeReqVO vo) {
        return ResponseUtil.ok(financeComputeDubboService.computeFinancingPrincipal(vo));
    }

    @GetMapping("/queryColumnModel")
    @ApiOperation(value = "查询列表模板")
    public ApiResult queryColumnModel(String type) {
        return ResponseUtil.ok(financeComputeDubboService.queryColumnModel(type));
    }

    @GetMapping("/queryPriceByCatalogCode")
    @ApiOperation(value = "根据配置查询价格")
    public ApiResult queryPriceByCatalogCode(String catalogCode) {
        return ResponseUtil.ok(financeComputeDubboService.queryPriceByCatalogCode(catalogCode));
    }
}
